package cn.lizemin.fakedata.project.elementUi.service.impl;

import cn.lizemin.fakedata.common.BusinessException;
import cn.lizemin.fakedata.common.ResultEnum;
import cn.lizemin.fakedata.project.elementUi.DTO.ElementUserDTO;
import cn.lizemin.fakedata.project.elementUi.entity.ElementUser;
import cn.lizemin.fakedata.project.elementUi.mapper.ElementUserMapper;
import cn.lizemin.fakedata.project.elementUi.service.IElementUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 学习element ui时需要用到的用户表 服务实现类
 * </p>
 *
 * @author lizemin
 * @since 2024-01-18
 */
@Service
public class ElementUserServiceImpl extends ServiceImpl<ElementUserMapper, ElementUser> implements IElementUserService {

    @Override
    public void addUser(ElementUserDTO user) {
        ElementUser elementUser = new ElementUser();
        BeanUtils.copyProperties(user, elementUser);
        elementUser = elementUser.create();
        this.save(elementUser);
    }

    @Override
    public void deleteUser(Integer id) {
        boolean success = this.removeById(id);
        if (!success) {
            throw new BusinessException(ResultEnum.USER_NOT_EXITS);
        }
    }

    @Override
    public void updateUser(ElementUserDTO user) {
        ElementUser elementUser = new ElementUser();
        BeanUtils.copyProperties(user, elementUser);
        elementUser.setUpdateTime(LocalDateTime.now());
        this.updateById(elementUser);
    }

    @Override
    public PageDTO<ElementUser> pageListUser(long currentPage, long pageSize) {
        PageDTO<ElementUser> pageDTO = new PageDTO<>(currentPage, pageSize);
        return this.page(pageDTO);
    }

}
